package com.zb.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.ZjFactoryProduct;
import com.zb.extend.FactoryProductExtend;
import com.zb.mapper.ZjFactoryProductMapper;
import com.zb.service.ZjFactoryProductService;
import com.zb.vo.FactoryProductVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 厂商产品关联表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2024-04-13
 */
@Service
public class ZjFactoryProductServiceImpl extends ServiceImpl<ZjFactoryProductMapper, ZjFactoryProduct> implements ZjFactoryProductService {
    @Resource
    private ZjFactoryProductMapper factoryProductMapper;

    // 保存品牌商家与产品关联
    @Override
    public void save(Long custAccId, Long productId, List<Long> factoryIds) {
        deleteByProductId(productId);
        if (CollectionUtil.isEmpty(factoryIds)) {
            return;
        }
        List<ZjFactoryProduct> factoryProducts = new ArrayList<>();
        ZjFactoryProduct factoryProduct = null;
        for (Long factoryId : factoryIds) {
            factoryProduct = new ZjFactoryProduct();
            factoryProduct.setId(IdWorker.getId());
            factoryProduct.setCustAccId(custAccId);
            factoryProduct.setFactoryId(factoryId);
            factoryProduct.setProductId(productId);
            factoryProducts.add(factoryProduct);
        }
        this.saveBatch(factoryProducts);
    }

    // 批量保存关联品牌商家商品
    @Override
    @Transactional
    public void save(Long custAccId, FactoryProductVo factoryProductVo) {
        if (CollectionUtil.isEmpty(factoryProductVo.getFactoryIds())) {
            return;
        }
        QueryWrapper<ZjFactoryProduct> wrapper = new QueryWrapper<>();
        wrapper.select("factory_id", "product_id")
                .in("factory_id", factoryProductVo.getFactoryIds());
        List<ZjFactoryProduct> factoryProductList = this.list(wrapper);

        List<ZjFactoryProduct> factoryProducts = new ArrayList<>();
        ZjFactoryProduct fp = null;
        if (CollectionUtil.isEmpty(factoryProductList)) {
            for (Long factoryId : factoryProductVo.getFactoryIds()) {
                for (Long productId : factoryProductVo.getProductIds()) {
                    fp = new ZjFactoryProduct();
                    fp.setId(IdWorker.getId())
                            .setCustAccId(custAccId)
                            .setFactoryId(factoryId)
                            .setProductId(productId);
                    factoryProducts.add(fp);
                }
            }
        } else {
            for (Long factoryId : factoryProductVo.getFactoryIds()) {
                for (Long productId : factoryProductVo.getProductIds()) {
                    if (factoryProductList.stream().anyMatch(f -> Objects.equals(f.getFactoryId(), factoryId) && !Objects.equals(f.getProductId(), productId))) {
                        fp = new ZjFactoryProduct();
                        fp.setId(IdWorker.getId())
                                .setCustAccId(custAccId)
                                .setFactoryId(factoryId)
                                .setProductId(productId);
                        factoryProducts.add(fp);
                    }
                }
            }
        }
        if (CollectionUtil.isNotEmpty(factoryProducts)) this.saveBatch(factoryProducts);
    }

    // 根据产品ID删除关联品牌商家
    @Override
    public void deleteByProductId(Long productId) {
        QueryWrapper<ZjFactoryProduct> wrapper = new QueryWrapper<>();
        wrapper.eq("product_id", productId);
        this.remove(wrapper);
    }

    // 根据产品ID删除关联品牌商家
    @Override
    public void deleteByProductIds(List<Long> productIds) {
        if (CollectionUtil.isEmpty(productIds)) {
            return;
        }
        QueryWrapper<ZjFactoryProduct> wrapper = new QueryWrapper<>();
        wrapper.in("product_id", productIds);
        this.remove(wrapper);
    }

    // 根据产品ID获取关联产品的品牌厂商
    @Override
    public List<ZjFactoryProduct> getByProductId(Long productId) {
        QueryWrapper<ZjFactoryProduct> wrapper = new QueryWrapper<>();
        wrapper.select("id", "factory_id")
        .eq("product_id", productId);
        return this.list(wrapper);
    }

    // 根据产品ID获取关联的商家
    @Override
    public List<FactoryProductExtend> getByProductIds(List<Long> productIds) {
        if (CollectionUtil.isEmpty(productIds)) {
            return new ArrayList<>();
        }
        return factoryProductMapper.getByProductIds(productIds);
    }
}
